#ifndef MYDIRECTIONBUTTON_H
#define MYDIRECTIONBUTTON_H

#include<QWidget>
#include<QPainter>
#include<QPainterPath>
#include<QVector>
#include<QSet>
#include<QPushButton>

class StyleObject;


namespace RO_Robot{

class ControlTimer;

class DirectionalPad : public QWidget
{
    Q_OBJECT

public:
    enum Direction {
        E_None, E_Up, E_Down, E_LeftRotate, E_RightRotate, E_Home
    };

    enum HomeBtn{
        E_Outer,E_Iner,E_Min_Rect
    };

    DirectionalPad(QWidget *parent = nullptr);
    ~DirectionalPad();
signals:
    void sigDirectionLeftRotate(QString key);
    void sigDirectionRightRotate(QString key);
    void sigDirectionUp(QString key);
    void sigDirectionDown(QString key);
    void sigPause(QString key);

    void sigReleaseDirectionLeftRotate(QString key);
    void sigReleaseDirectionRightRotate(QString key);
    void sigReleaseDirectionUp(QString key);
    void sigReleaseDirectionDown(QString key);
    void sigReleasePause(QString key);

public slots:
    void slotKeyChecked(const QString &key);
    void slotKeyUnChecked(const QString &key);

protected:
    void mousePressEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void leaveEvent(QEvent *event) override;

    void paintEvent(QPaintEvent *event)override;
    void resizeEvent(QResizeEvent *event) override;
    // void keyPressEvent(QKeyEvent *event) override;
    // void keyReleaseEvent(QKeyEvent *event) override;

public:
    double _scale;
    QVector<QPainterPath>_buttons;             //按钮区域
    QVector<QPainterPath>_outArcBtn;           //绘制外弧实线
    QVector<QPainterPath>_triangle;            //小三角形
    QVector<QPainterPath>_centerButtons;       //中心按钮

    QSet<Direction> _keyboardPressedBtn;       // 键盘按下的按钮集合
    Direction _mousePressedBtn = E_None;       // 鼠标按下的按钮（互斥)
    Direction _mouseMoveBtn = E_None;          //鼠标移动按钮

    qreal _triangleSize;                       // 三角形边长
    qreal _triangleWidth;                      // 正三角形高度
    qreal _triangleHeight;                     //正三角形宽度

    void createButtons();                      //创建按钮路径
    void drawDirectionalButtons(QPainter &painter);
    void drawDirectionalPressButtons(QPainter &painter);
    void drawTriangle(QPainter &painter);
    void drawPressTriangles(QPainter &painter);
    void drawCenterButtons(QPainter &painter);
    Direction getDirectionFromPoint(const QPointF &point);
    bool isButtonPressed(Direction dir) const; // 判断按钮是否被按下
};
}
#endif // MYDIRECTIONBUTTON_H
